#pragma once

#include <iostream>
#include <pthread.h>
#include <cassert>

class Mutex
{
public:
    Mutex(pthread_mutex_t *pmutex = nullptr) : _pmutex(pmutex)
    {
    }

    void *lock()
    {
        if (_pmutex)
        {
            pthread_mutex_lock(_pmutex);
        }
        else
        {
            std::cout << "互斥量未初始化！！" << std::endl;
        }
    }
    void *unlock()
    {
        if (_pmutex)
        {
            pthread_mutex_unlock(_pmutex);
        }
        else
        {
            std::cout << "互斥量未初始化！！" << std::endl;
        }
    }

private:
    pthread_mutex_t *_pmutex;
};

class LockGuard
{
public:
    LockGuard(pthread_mutex_t* mutex = nullptr):_mutex(mutex)
    {
        _mutex.lock();//在构造函数中加锁
    }
    ~LockGuard()
    {
        _mutex.unlock();//在析构函数中解锁
    }
private:
    Mutex _mutex;
};
